package com.flower.practice.service.impl;

import com.flower.practice.dao.SubjectCategoryDao;
import com.flower.practice.dao.SubjectLabelDao;
import com.flower.practice.dao.SubjectMappingDao;
import com.flower.practice.entity.dto.CategoryDTO;
import com.flower.practice.entity.po.CategoryPO;
import com.flower.practice.entity.po.LabelCountPO;
import com.flower.practice.entity.po.PrimaryCategoryPO;
import com.flower.practice.entity.po.SubjectLabelPO;
import com.flower.practice.enums.SubjectInfoTypeEnum;
import com.flower.practice.service.PracticeSetService;
import com.flower.practice.vo.SpecialPracticeCategoryVO;
import com.flower.practice.vo.SpecialPracticeLabelVO;
import com.flower.practice.vo.SpecialPracticeVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-08-02 21:23
 */
@Service
public class PracticeSetServiceImpl implements PracticeSetService {

    @Resource
    private SubjectCategoryDao subjectCategoryDao;

    @Resource
    private SubjectMappingDao subjectMappingDao;

    @Resource
    private SubjectLabelDao subjectLabelDao;


    @Override
    public List<SpecialPracticeVO> getSpecialPracticeContent() {
        List<SpecialPracticeVO> specialPracticeVOList = new LinkedList<>();
        List<Integer> subjectTypeList = new ArrayList<>();
        subjectTypeList.add(SubjectInfoTypeEnum.RADIO.getCode());
        subjectTypeList.add(SubjectInfoTypeEnum.MULTIPLE.getCode());
        subjectTypeList.add(SubjectInfoTypeEnum.JUDGE.getCode());
        subjectTypeList.add(SubjectInfoTypeEnum.BRIEF.getCode());

        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setSubjectTypeList(subjectTypeList);

        // 获取所有的大类
        List<PrimaryCategoryPO> poList =  subjectCategoryDao.getPrimaryCategory(categoryDTO);
        if(CollectionUtils.isEmpty(poList)) {

        }

        poList.forEach(primaryCategoryPO ->{
            SpecialPracticeVO specialPracticeVO = new SpecialPracticeVO();
            specialPracticeVO.setPrimaryCategoryId(primaryCategoryPO.getParentId());
            CategoryPO categoryPO = subjectCategoryDao.selectById(primaryCategoryPO.getId());
            specialPracticeVO.setPrimaryCategoryName(categoryPO.getCategoryName());
            CategoryDTO categoryDTOTemp = new CategoryDTO();
            categoryDTOTemp.setCategoryType(2);
            categoryDTOTemp.setParentId(primaryCategoryPO.getParentId());
            List<CategoryPO> smallPoList =  subjectCategoryDao.selectList(categoryDTOTemp);
            if(CollectionUtils.isEmpty(smallPoList)){
                return;
            }
            List<SpecialPracticeCategoryVO> categoryList = new LinkedList<>();
            smallPoList.forEach(smallPo ->{
                List<SpecialPracticeLabelVO> labelVOList = getLabelVOList(smallPo.getId(), subjectTypeList);
                if(CollectionUtils.isEmpty(labelVOList)){
                    return;
                }
                SpecialPracticeCategoryVO specialPracticeCategoryVO = new SpecialPracticeCategoryVO();
                specialPracticeCategoryVO.setCategoryId(smallPo.getId());
                specialPracticeCategoryVO.setCategoryName(smallPo.getCategoryName());

                List<SpecialPracticeLabelVO> labelList = new LinkedList<>();

                labelVOList.forEach(labelVO->{
                    SpecialPracticeLabelVO specialPracticeLabelVO = new SpecialPracticeLabelVO();
                    specialPracticeLabelVO.setId(labelVO.getId());
                    specialPracticeLabelVO.setAssembleId(labelVO.getAssembleId());
                    specialPracticeLabelVO.setLabelName(labelVO.getLabelName());
                    labelList.add(specialPracticeLabelVO);
                });

                specialPracticeCategoryVO.setLabelList(labelList);
                categoryList.add(specialPracticeCategoryVO);
            });
            specialPracticeVO.setCategoryVOList(categoryList);
            specialPracticeVOList.add(specialPracticeVO);
        });


        return specialPracticeVOList;
    }

    private List<SpecialPracticeLabelVO> getLabelVOList(Long categoryId, List<Integer> subjectTypeList) {

       List<LabelCountPO> countPOList = subjectMappingDao.getLabelSubjectCount(categoryId, subjectTypeList);

        if (CollectionUtils.isEmpty(countPOList)) {
            return Collections.emptyList();
        }
        List<SpecialPracticeLabelVO> labelVOList = new LinkedList<>();
        countPOList.forEach(countPO ->{
            SpecialPracticeLabelVO specialPracticeLabelVO = new SpecialPracticeLabelVO();
            specialPracticeLabelVO.setId(countPO.getLabelId());
            specialPracticeLabelVO.setAssembleId(categoryId + "-" + countPO.getLabelId());

            SubjectLabelPO subjectLabelPO = subjectLabelDao.queryById(countPO.getLabelId());
            specialPracticeLabelVO.setLabelName(subjectLabelPO.getLabelName());
            labelVOList.add(specialPracticeLabelVO);
        });
        return labelVOList;
    }
}
